﻿using System.ComponentModel;
using System.Windows;

namespace TestRunner 
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow:INotifyPropertyChanged
    {
        private string nUnitFolderPath;

        public string NUnitFolderPath
        {
            get { return nUnitFolderPath; }
            set
            {
                nUnitFolderPath = value;
                var handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs("NUnitFolderPath"));
                }
            }
        }

        public SettingsWindow() 
        {
            InitializeComponent();
            DataContext = this;
            NUnitFolderPath = App.Settings.NUnitFolderPath;
            if (App.Settings.NUnitIs32Bit)
            {
                bitness32RadioButton.IsChecked = true;
            }
            else
            {
                bitness64RadioButton.IsChecked = true;
            }
        }

        private void OnBrowseNUnitFolderPath(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "Select NUnit folder";
            dialog.SelectedPath = NUnitFolderPath;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                NUnitFolderPath = dialog.SelectedPath;
            }
        }

        private void OnOKButton(object sender, RoutedEventArgs e) 
        {
            NUnitFolderNameValidationRule rule = new NUnitFolderNameValidationRule();
            if (rule.Validate(NUnitFolderPath, null).IsValid)
            {
                App.Settings.NUnitFolderPath = NUnitFolderPath;

                App.Settings.NUnitIs32Bit = bitness32RadioButton.IsChecked.HasValue && bitness32RadioButton.IsChecked.Value;
                Close();
            }
        }

        private void OnCancelButton(object sender, RoutedEventArgs e) 
        {
            Close();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
